package com.zhengshang.api.controller;

import com.zhengshang.api.domain.AppGame;
import com.zhengshang.api.service.IAppGameService;
import com.zhengshang.common.core.controller.BaseController;
import com.zhengshang.common.core.domain.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author zyf
 */
@Api(tags = "游戏接口")
@RestController
@RequestMapping("/game")
public class AppGameController extends BaseController {

    @Autowired
    private IAppGameService appGameService;




    @ApiOperation(value = "游戏列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码（从1开始）", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数（最小为1，最大为100）", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "gameType", value = "游戏类型（1安卓，2Ios，3H5）", required = true, dataType = "String", paramType = "query")
    })
    @PostMapping("/list")
    public AjaxResult orderList(
            @RequestParam Integer pageNum,
            @RequestParam Integer gameType,
            @RequestParam(required = false) Integer pageSize,
            HttpServletRequest request, HttpServletResponse response){

        if (gameType==null||pageNum == null || pageNum.intValue() <= 0 || (pageSize != null && pageSize.intValue() < 1)) {
            return AjaxResult.error("参数错误");
        }
        if (pageSize == null) {
            pageSize = 10;
        }
        if (pageSize.intValue() > 100) {
            pageSize = 100;
        }
        try {
            AppGame appGame = new  AppGame();
            appGame.getParams().put("pageNum", pageNum);
            appGame.getParams().put("pageSize", pageSize);
            appGame.setGameType(gameType);
            List<AppGame> appGames = appGameService.queryAppGameList(appGame);
            return AjaxResult.success(appGames);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error("查询失败");
    }

}
